package com.arcbees.test.collaborative.client.util;

import com.arcbees.test.collaborative.client.dispatch.AsyncCallbackImpl;
import com.arcbees.test.collaborative.shared.LoggingMessage;
import com.arcbees.test.collaborative.shared.LoggingMessage.LogLevel;
import com.arcbees.test.collaborative.shared.NoResult;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.shared.DispatchAsync;

public class ClientLogger {

  private final DispatchAsync dispatcher;

  @Inject
  public ClientLogger(DispatchAsync dispatcher) {
    this.dispatcher = dispatcher;
  }

  public void log(LogLevel level, String message) {
    StackTraceElement[] stack = new Throwable().getStackTrace();
    String method = stack.length >= 1 ? stack[1].toString() : "";
    LoggingMessage logMsg = new LoggingMessage(level, method + " " + message);
    dispatcher.execute(logMsg, new AsyncCallbackImpl<NoResult>() {
      @Override
      public void onSuccess(NoResult result) {
      }
    });
  }
}
